package com.aimer.activity;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.view.View;


import com.aimer.adapter.HomeAdapter;

import com.aimer.dialog.AnnouncementDialog;
import com.aimer.model.HomeModel;

import com.anime.R;
import com.anime.activity.AnimeSearchRecordActivity;
import com.anime.databinding.ActivityMainBinding;
import com.anime.fragment.AnimeFragment;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.books.activity.BooksSearchRecordActivity;



import com.books.fragment.BooksFragment;
import com.cartoon.fragment.*;
import com.cartoon.activity.CartoonSearchRecordActivity;

import com.center.fragment.CenterFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tencent.bugly.crashreport.CrashReport;


import java.util.ArrayList;

import base.QActivity;
import constant.AppConstants;


public class MainActivity extends QActivity {
    private ActivityMainBinding binding;
    private String searchContent;
    private HomeAdapter homeAdapter;
    private ArrayList<HomeModel> homelist = new ArrayList<>();

    private AnimeFragment animeFragment = new AnimeFragment();
    private CartoonFragment cartoonFragment = new CartoonFragment();
  private BooksFragment booksFragment = new BooksFragment();
//private AimerFragment booksFragment = new AimerFragment();

    private CenterFragment centerFragment = new CenterFragment();


    @Override
    public int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {

        binding = DataBindingUtil.setContentView(this, getLayoutRes());

        AnnouncementDialog dialog = new AnnouncementDialog();
        dialog.show(getSupportFragmentManager(), "");
    }

    @Override
    public void initData() {


        homelist.add(new HomeModel("视频", R.mipmap.video1, R.mipmap.video2));
        homelist.add(new HomeModel("漫画", R.mipmap.cartoon1, R.mipmap.cartoon2));
        homelist.add(new HomeModel("小说", R.mipmap.books1, R.mipmap.books2));
        homelist.add(new HomeModel("个人", R.mipmap.center1, R.mipmap.center2));
        GridLayoutManager layoutManager = new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rvHome.setLayoutManager(layoutManager);
        homeAdapter = new HomeAdapter(R.layout.home_item, homelist);
        binding.rvHome.setAdapter(homeAdapter);

        FragmentUtils.replace(getSupportFragmentManager(), animeFragment, R.id.fragment);

        //设置不获取焦点
        binding.edSearch.setFocusable(false);





    }

    @Override
    public void initListener() {
        binding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.d("获取当前fragment " + FragmentUtils.getTopShow(getSupportFragmentManager()).getClass().getName());
                if (FragmentUtils.getTopShow(getSupportFragmentManager()).getClass().getName().equals("com.anime.fragment.AnimeFragment")) {
                    Intent integer = new Intent(MainActivity.this, AnimeSearchRecordActivity.class);
                    startActivity(integer);
                }
                if (FragmentUtils.getTopShow(getSupportFragmentManager()).getClass().getName().equals("com.cartoon.fragment.CartoonFragment")) {
                    Intent integer = new Intent(MainActivity.this, CartoonSearchRecordActivity.class);
                    startActivity(integer);
                } else if (FragmentUtils.getTopShow(getSupportFragmentManager()).getClass().getName().equals("com.books.fragment.BooksFragment")) {

                }
            }
        });

        binding.edSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.d("获取当前fragment " + FragmentUtils.getTopShow(getSupportFragmentManager()).getClass().getName());
                if (FragmentUtils.getTopShow(getSupportFragmentManager()).getClass().getName().equals("com.anime.fragment.AnimeFragment")) {
                    Intent integer = new Intent(MainActivity.this, AnimeSearchRecordActivity.class);
                    startActivity(integer);
                }
                if (FragmentUtils.getTopShow(getSupportFragmentManager()).getClass().getName().equals("com.cartoon.fragment.CartoonFragment")) {
                    Intent integer = new Intent(MainActivity.this, CartoonSearchRecordActivity.class);
                    startActivity(integer);
                } else if (FragmentUtils.getTopShow(getSupportFragmentManager()).getClass().getName().equals("com.books.fragment.BooksFragment")) {
                    Intent integer = new Intent(MainActivity.this, BooksSearchRecordActivity.class);
                    startActivity(integer);

                }
            }
        });

        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                homeAdapter.setIndex(position);
                homeAdapter.notifyDataSetChanged();
                if (position == 0) {
                    binding.edSearch.setVisibility(View.VISIBLE);
                    binding.ivSearch.setVisibility(View.VISIBLE);
                    FragmentUtils.replace(getSupportFragmentManager(), animeFragment, R.id.fragment);
                } else if (position == 1) {
                    int anInt = SPStaticUtils.getInt(AppConstants.SWITCHKEY, 1);
                    if(anInt==1){
                        binding.ivSearch.setVisibility(View.VISIBLE);
                        binding.edSearch.setVisibility(View.VISIBLE);
                    }else if(anInt==2){
                        binding.ivSearch.setVisibility(View.GONE);
                        binding.edSearch.setVisibility(View.GONE);
                    }

            FragmentUtils.replace(getSupportFragmentManager(), cartoonFragment, R.id.fragment);

                } else if (position == 2) {
//                    ToastUtils.showLong("正在开发中");
                 FragmentUtils.replace(getSupportFragmentManager(), booksFragment, R.id.fragment);
//                    binding.edSearch.setVisibility(View.VISIBLE);
//                    binding.ivSearch.setVisibility(View.VISIBLE);
                } else if (position == 3) {
                    binding.edSearch.setVisibility(View.GONE);
                    binding.ivSearch.setVisibility(View.GONE);
                    FragmentUtils.replace(getSupportFragmentManager(), centerFragment, R.id.fragment);
                }
            }
        });


    }


    @Override
    public void networkMessage() {


    }
}